package com.dotto.atos.fragments;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.dotto.atos.R;
import com.dotto.atos.activity.HomeActivity;
import com.dotto.atos.ui.NoDefaultSpinner;
import com.dotto.atos.utils.MessageUtil;
import com.dotto.atos.utils.TextUtil;

public class OnlinePaymentFragment extends Fragment {
	public static final String TAG = "OnlinePaymentFragment";
	private View fragmentView;
	private Activity mActivity;
	@SuppressWarnings("unused")
	private OnlinePaymentFragment instance;

	private MessageUtil messageUtil;

	private NoDefaultSpinner spnPaymentType;
	private EditText inptName;
	private EditText inptSurname;
	private EditText inptAccount;
	private EditText inptAmount;
	private Button btnCheckPayment;
	private Button btnPay;
	public static String amount;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (fragmentView == null) {
			fragmentView = inflater.inflate(R.layout.fragment_online_payment,
					container, false);
		} else {
			((ViewGroup) fragmentView.getParent()).removeView(fragmentView);
		}
		mActivity = this.getActivity();
		instance = this;
		messageUtil = new MessageUtil(mActivity);
		initView();
		return fragmentView;
	}

	public void initView() {
		if (fragmentView != null) {
			spnPaymentType = (NoDefaultSpinner) fragmentView
					.findViewById(R.id.online_payment_spn_payment_type);
			inptName = (EditText) fragmentView
					.findViewById(R.id.online_payment_inpt_name);
			inptSurname = (EditText) fragmentView
					.findViewById(R.id.online_payment_inpt_sur_name);
			inptAccount = (EditText) fragmentView
					.findViewById(R.id.online_payment_inpt_account);
			inptAmount = (EditText) fragmentView
					.findViewById(R.id.online_payment_inpt_amount);
			btnCheckPayment = (Button) fragmentView
					.findViewById(R.id.online_payment_btn_check_payment);
			btnPay = (Button) fragmentView
					.findViewById(R.id.online_payment_btn_pay);

			spnPaymentType.setSelection(-1);
			inptName.setText("");
			inptAccount.setText("");
			inptSurname.setText("");
			inptAmount.setText(null);
			inptAmount.setFocusable(false);
			inptAmount.setFocusableInTouchMode(false);
			inptAmount.setClickable(false);
			btnCheckPayment.setEnabled(true);

			btnCheckPayment.setOnClickListener(onClickCheckPayment);
			btnPay.setOnClickListener(onClickPay);
		}
	}

	private OnClickListener onClickPay = new OnClickListener() {

		@Override
		public void onClick(View v) {
			HomeActivity.instance.hideKeyboard(inptAccount);
			spnPaymentType.setBackgroundResource(R.drawable.edittext);
			inptAccount.setBackgroundResource(R.drawable.edittext);
			int selected = spnPaymentType.getSelectedItemPosition();
			if (selected > 0) {
				String account = inptAccount.getText().toString();
				if (!TextUtil.isEmpty(account)) {
					amount = inptAmount.getText().toString();
					HomeActivity.instance.showCheckOutFragment(1);
				} else {
					messageUtil
							.showToastMessage(getString(R.string.online_payment_account_empty));
					inptAccount
							.setBackgroundResource(R.drawable.edittext_error);
				}
			} else {
				spnPaymentType.setBackgroundResource(R.drawable.edittext_error);
				messageUtil
						.showToastMessage(getString(R.string.online_payment_pament_types_empty));
			}
		}
	};

	private OnClickListener onClickCheckPayment = new OnClickListener() {

		@Override
		public void onClick(View v) {
			HomeActivity.instance.hideKeyboard(inptAccount);
			spnPaymentType.setBackgroundResource(R.drawable.edittext);
			int selected = spnPaymentType.getSelectedItemPosition();
			if (selected > 0) {
				inptAmount.setText(((new Random()).nextInt(9999 - 1000) + 1000)
						+ "");
				btnCheckPayment.setEnabled(false);
			} else {
				spnPaymentType.setBackgroundResource(R.drawable.edittext_error);
				messageUtil
						.showToastMessage(getString(R.string.online_payment_pament_types_empty));
			}
		}
	};
}
